﻿using Infrastructures;
using Models;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Bson;
using IRepository;
using MongoDB.Driver.Builders;

namespace RepositoryMangoDB
{
    public class LogRepository : ILogRepository
    {
        internal LogRepository() { }

        private MongoCollection collection = DBHelper.MangoDBInstance.GetCollection<EventLog>("EventLog");

        public void Create(EventLog log)
        {
            collection.Insert(log);
        }

        public List<EventLog> List(string eventID)
        {
            var query = Query<EventLog>.Where(x => x.EventID == eventID);
            return collection.FindAs<EventLog>(query).OrderByDescending(x => x.ActionTime).ToList();
        }

        public List<EventLog> List(EventType type, string eventObj)
        {
            var query = Query<EventLog>.Where(x => x.EventObject == eventObj && x.ActionType == type);
            return collection.FindAs<EventLog>(query).OrderByDescending(x => x.ActionTime).ToList();
        }
    }
}
